
const app = getApp();

import Toast, { hideToast } from 'tdesign-miniprogram/toast/index';
Page({
  data: {
    weizi: [],
    resultList: [],
    value: '',
    pageParams: {
      pageNo: 1,
      pageSize: 8,
      params: {
        name: '',
        type: null,
        introduction: ''
      }
    },
    list: [],
    swiperList:[],
    hasMore: true,
    imageProps: {
      mode: 'aspectFill'
    },
    swiperConfig: {
      current: 0,
      autoplay: true,
      duration: 500,
      interval: 5000
    },
  },

  onLoad() {
    this.findPage(false);
  },
  onShow() {
    this.getTabBar().init();
  },
  findPage(refresh) {
    this.showToast('加载中...', true);
    app.httpPost('mini/home/list', this.data.pageParams).then((res) => {
      hideToast({
        context: this,
        selector: '#t-toast',
      });
      const { current, pages, records } = res.data;
      if (current >= pages) {
        this.setData({ hasMore: false });
      }
      if (records && records.length > 0) {
        let list = refresh ? records : this.data.list.concat(records);
        list.forEach(item => {
          item.imageUrl = app.fileView(item.applicableAge);
        });
        this.setData({ list });
        // 更新轮播图数据
        const swiperList = list.map((item) => {
          return {
            image: item.imageUrl,
          };
        });
        this.setData({ swiperList });
        // 将swiperList的image值存放到weizi数组中
        const weizi = swiperList.slice(0, 4).map(item => item.image);
        this.setData({ weizi });
      } else {
        this.showToast('已经到底了~');
      }
    });
  },
  
  onPullDownRefresh() {
    const pageParams = {
      pageNo: 1,
      pageSize: 8,
      params: {
        name: '',
        type: null,
        introduction: ''
      }
    };
    this.setData({ hasMore: true, pageParams });
    this.findPage(true);
    wx.stopPullDownRefresh();
  },
  onReachBottom() {
    if (!this.data.hasMore) {
      this.showToast('已经到底了~');
      return;
    }
    this.data.pageParams.pageNo = this.data.pageParams.pageNo + 1;
    this.findPage(false);
  },
  showToast(str, isClose = false) {
    const obj = {
      context: this,
      selector: '#t-toast',
      message: `${str}~`,
    };
    if (isClose) {
      obj.duration = -1;
    }
    Toast(obj);
  },
  onChangeValue(e) {
    const { value } = e.detail;
    if (value) {
      // 根据用户输入过滤列表，并截取name字段中关键字周围5个字符
      const filteredList = this.data.list.filter(item => item.name.includes(value)).map(item => {
        const index = item.name.indexOf(value);
        const start = Math.max(0, index - 5);
        const end = Math.min(item.name.length, index + value.length + 5);
        const highlightedText = `<text style="color: red;">${value}</text>`;
        return {
          ...item,
          text: `${item.name.substring(start, index)}${highlightedText}${item.name.substring(index + value.length, end)}`,
        };
      });
      this.setData({
        list: filteredList, // 更新list数据
        value: value // 更新搜索框的值
      });
    } else {
      // 如果搜索框为空，则重置list为原始数据
      this.findPage(true); // 假设findPage方法可以重新加载原始数据
      this.setData({
        value: '' // 清空搜索框的值
      });
    }
  },

  onSearchResultTap(e) {
    const { id } = e.currentTarget.dataset;
    // 构建详情页面的 URL
    const detailPagePath = `/pages/index/detail/detail?id=${id}`;
    // 使用 wx.navigateTo 进行页面跳转
    wx.navigateTo({
      url: detailPagePath
    });
  },
  
  onSearchResultTap(e) {
    const { id } = e.currentTarget.dataset;
    // 构建详情页面的 URL
    const detailPagePath = `/pages/index/detail/detail?id=${id}`;
    // 使用 wx.navigateTo 进行页面跳转
    wx.navigateTo({
      url: detailPagePath
    });
  },
  onGridItemTap: function(e) {
    const id = e.currentTarget.dataset.id;
    // 构建详情页面的 URL，假设详情页面的路径是 `/pages/detail/detail`
    const detailPagePath = `/pages/index/detail/detail?id=${id}`;
    // 使用 wx.navigateTo 进行页面跳转
    wx.navigateTo({
      url: detailPagePath
    });
  },
});
